package com.ruoyi.security.elevator.websocket;

import com.ruoyi.security.service.IElevatorSendService;
import com.ruoyi.system.api.enums.ElevatorValue;
import org.java_websocket.client.WebSocketClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.net.URISyntaxException;

@Component
@Order(-1)
public class WebSocketStartBoot implements CommandLineRunner {

    @Autowired
    private IElevatorSendService sendService;


    public void startElevatorWebSocket(URI uri) throws URISyntaxException {
        WebSocketClient webSocketClient = new ElevatorWebsocketClient(uri);
        webSocketClient.connect();
        WebSocketSessionManger.putSession(uri.toString(), webSocketClient);
        //webSocketClient.onClose();

        // 查询数据库 已经设置好的电梯 发送电梯命令
        //sendService.sendGetElevatorRealData("1",webSocketClient);

    }


    @Override
    public void run(String... args) throws Exception {
        startElevatorWebSocket(ElevatorValue.WEBSOCKET_URI);
    }
}
